package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysDepartmentInfo;
import com.emep.changzhi.analyse.entity.basics.SysLeadershipInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.LeaderService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import com.emep.changzhi.analyse.utils.MapToObject;
import com.emep.changzhi.analyse.utils.PageChunk;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/9/3 10:47
 */
@Api(value = "环保局领导模块")
@RestController
@RequestMapping(value = "/leader")
public class LeaderController {

    @Autowired
    LeaderService leaderService;
    MapToObject<SysLeadershipInfo> mapToObject=new MapToObject<>();

    @ApiOperation(value = "查询环保局领导列表", notes = "传入若干参数")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean list(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "condition", "pageIndex", "pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            int pageIndex = Integer.parseInt(payload.get("pageIndex").toString());
            int pageSize = Integer.parseInt(payload.get("pageSize").toString());
            Map<String, Object> condition = (Map<String, Object>) payload.get("condition");
            Page<SysLeadershipInfo> leaderList = leaderService.find(pageIndex, pageSize, condition);
            PageChunk<SysLeadershipInfo> pageChunk = new PageChunk<>();
            if(leaderList != null){
                return result.boolAdd(pageChunk.pageChunk(leaderList));
            }else {
                return result.error("未查到领导列表");
            }
        }
        return result;
    }

    @ApiOperation(value = "查询环保局领导详情", notes = "传入若干参数")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean info(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "leaderId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Long leaderId = Long.parseLong(payload.get("leaderId").toString());
            SysLeadershipInfo leader = leaderService.getLeader(leaderId);
            if (leader != null) {
                return result.boolAdd(leader);
            }else {
                return result.error("未查到此领导");
            }
        }
        return result;
    }

    @ApiOperation(value = "更新环保局领导信息", notes = "传入若干参数")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean update(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "leader");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("leader");
                Long leaderId = Long.parseLong(map.get("leaderId").toString());
                if(leaderId !=null) {
                    SysLeadershipInfo leader = leaderService.getLeader(leaderId);
                    if (leader != null) {
                        boolean b = mapToObject.convertEntity(leader, map);
                        if (b) {
                            boolean a = leaderService.updateLeader(leader);
                            if (a) {
                                return result.boolAdd("更新成功");
                            } else {
                                return result.error("该名称已存在");
                            }
                        } else {
                            return result.error("无此领导");
                        }
                    } else {
                        return result.error("参数错误");
                    }
                }else {
                    return result.error("leaderId为必填参数");
                }

        }
        return result;
    }

    @ApiOperation(value = "添加领导", notes = "传入若干参数")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean add(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "leader");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("leader");
            SysLeadershipInfo leader = new SysLeadershipInfo();
            boolean b= mapToObject.convertEntity(leader, map);
            if(b) {
                boolean a = leaderService.add(leader);
                if (a ) {
                    return result.boolAdd("新增失败");
                } else {
                    return result.error("该名称已存在");
                }
            }else {
                return result.error("参数错误");
            }
        }
        return result;
    }

    @ApiOperation(value = "删除领导", notes = "传入若干参数")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean delete(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "leaderId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Long leader = Long.parseLong(payload.get("leaderId").toString());
            if(leader!=null) {
                SysLeadershipInfo sysLeadershipInfo = leaderService.getLeader(leader);
                if (sysLeadershipInfo != null) {
                    if(!sysLeadershipInfo.getIsDelete().equals(Constant.RESULT_DELETECODE_YES)){
                        sysLeadershipInfo.setIsDelete(Constant.RESULT_DELETECODE_YES);
                        boolean a = leaderService.deleteLeader(sysLeadershipInfo);
                        if (a) {
                            return result.boolAdd("删除成功");
                        } else {
                            return result.boolAdd("删除失败");
                        }
                    }else {
                        return result.error("此领导状态为已删除,删除失败");
                    }
                } else {
                    return result.boolAdd("无此领导");
                }
            }else {
                return result.error("leaderId为必填参数");
            }
        }
        return result;
    }
}
